<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Cache;

class RecruitController extends Controller
{
    public $appId = 'wx5d11a563a1d8156a';
    public $appSecret = 'b49eb91e40ad0ef8c93d6fa80c9a587f';
    public $access_token = Null;
    public $ticket = Null;

    protected function index(Request $request)
    {
        $this->getMpAccessToken();
        $this->getMpTicket();
        $nonceStr = $this->createNonceStr();
        $timestamp = time();

        $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $rawString = "jsapi_ticket=".$this->ticket."&noncestr=".$nonceStr."&timestamp=".$timestamp."&url=".$url;
        $signature = sha1($rawString);
        $signPackage = array(
            "appId"     => $this->appId,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $rawString
        );
        return view('recruit.index')->with('signPackage', $signPackage);
    }

    public function ad180328(Request $request)
    {
        $this->getMpAccessToken();
        $this->getMpTicket();
        $nonceStr = $this->createNonceStr();
        $timestamp = time();

        $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $rawString = "jsapi_ticket=".$this->ticket."&noncestr=".$nonceStr."&timestamp=".$timestamp."&url=".$url;
        $signature = sha1($rawString);
        $signPackage = array(
            "appId"     => $this->appId,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $rawString
        );
        return view('recruit.ad180328')->with('signPackage', $signPackage);
    }

    public function ad180330(Request $request)
    {
        $this->getMpAccessToken();
        $this->getMpTicket();
        $nonceStr = $this->createNonceStr();
        $timestamp = time();

        $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $rawString = "jsapi_ticket=".$this->ticket."&noncestr=".$nonceStr."&timestamp=".$timestamp."&url=".$url;
        $signature = sha1($rawString);
        $signPackage = array(
            "appId"     => $this->appId,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $rawString
        );
        return view('recruit.ad180330')->with('signPackage', $signPackage);
    }

    protected function createNonceStr($length = 16)
    {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    protected function getHttpContent($url)
    {
        try {
            $client = new \GuzzleHttp\Client();
            $response = $client->request('GET', $url);
            $htmlContent = $response->getBody()->getContents();
            return $htmlContent;

        } catch (\GuzzleHttp\Exception\ConnectException $e) {
            Log::error($e->getMessage());
            return null;
        } catch (\Exception $e) {
            Log::error($e->getMessage());
            return null;
        }

    }

    protected function getMpAccessToken()
    {
        //Cache::remember('mpAccessToken', 30, function () {
            $output = $this->getHttpContent("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appId . "&secret=" . $this->appSecret);
            $outjson = json_decode($output, true);
            if (!isset($outjson["access_token"])) {
                $this->access_token = null;
            } else {
                $this->access_token = $outjson["access_token"];
            }
        //});
    }

    protected function getMpTicket()
    {
        $accessToken = $this->access_token;
        //Cache::remember('mpTicket', 30, function () use ($accessToken) {
            $output = $this->getHttpContent("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$accessToken."&type=jsapi");
            $getTicket = json_decode($output, true);
            if(!isset($getTicket["ticket"])) {
                $this->ticket = Null;
            } else {
                $this->ticket = $getTicket['ticket'];
            }
        //});
    }
}
